﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.DataService;
using WebShop.Classes.DataSource;
using WebShop.Models;
using WebShop.Classes;

namespace WebShop.Controllers
{
    public class IndexController : BaseController
    {
        //
        // GET: /Index/

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            IDataSource dataSource = DataSourceFactory.Contruct();
            model.Categories = dataSource.GetCategories(0);
            model.Title = Translater.Translate("title_index");
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Product(int id)
        {
            ActionResult result = null;

            IDataSource dataSource = DataSourceFactory.Contruct();
            ProductFull item = dataSource.GetProduct(id);

            if (item != null)
            {
                ProductModel model = new ProductModel()
                {
                    Product = item,
                    Title = String.Format(Translater.Translate("title_product_{0}"), item.Name),
                    Breadcrumbs = this.GetCategoryBreadcrumbs(item.CategoryId)
                };
                result = View(model);
            }
            else
            {
                result = this.Error404();
            }

            return result;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Category(int id, int page = 0)
        {
            ActionResult result = null;

            IDataSource dataSource = DataSourceFactory.Contruct();
            List<DataService.CategoryView> list = dataSource.GetCategories(id);
            if (list != null)
            {
                //show two level
                if (list.Count != 0)
                {
                    result = this.CategoryList(id);
                } else { // whow products
                    result = this.CategoryShow(id, page);
                }
            } else {
                result = this.Error404();
            }

            return result;
        }

        private List<CategoryView> GetCategoryBreadcrumbs(int categoryId)
        {
            List<CategoryView> result = new List<CategoryView>();

            IDataSource dataSource = DataSourceFactory.Contruct();
            CategoryView caterory = dataSource.GetCategory(categoryId);
            if (caterory != null)
            {
                result.Add(caterory);
                if (caterory.ParentId.HasValue && caterory.ParentId != 0)
                {
                    caterory = dataSource.GetCategory(caterory.ParentId.Value);
                    if (caterory != null)
                    {
                        result.Add(caterory);
                    }
                }
            }
            result.Reverse();

            return result;
        }

        /// <summary>
        /// Show category what consist sub-categories
        /// </summary>
        /// <param name="id">Category id</param>
        /// <returns></returns>
        private ActionResult CategoryList(int id)
        {
            CategoryListModel model = new CategoryListModel();
            IDataSource dataSource = DataSourceFactory.Contruct();

            model.Categories = dataSource.GetCategories(id);
            

            CategoryView caterory = dataSource.GetCategory(id);

            if (caterory != null)
            {
                model.Breadcrumbs = this.GetCategoryBreadcrumbs(id);
                string category_name = caterory.Name;
                model.Title = String.Format(Translater.Translate("title_category_{0}"), category_name);
            }
            else
            {
                return this.Error404();
            }
            return View("CategoryList", model);
        }


        /// <summary>
        /// Show category with items
        /// </summary>
        /// <param name="id">Category id</param>
        /// <param name="page">Page number - begin from 0</param>
        /// <returns></returns>
        private ActionResult CategoryShow(int id, int page = 0)
        {
            ActionResult result = this.Error404();
            IDataSource dataSource = DataSourceFactory.Contruct();
            int pages = dataSource.GetCategoryPages(id);

            if (pages != 0)
            {
                CategoryShowModel model = new CategoryShowModel();
                model.Pages = pages;
                model.Page = page;

                model.Orders = new Dictionary<int, string>()
                                                    {
                                                        {1, model.Translate("priceAsc")},
                                                        {2, model.Translate("priceDesc")},
                                                        {3, model.Translate("nameAsc")},
                                                        {4, model.Translate("nameDesc")},
                                                    };

                ProductSort selected_order = ProductSort.NameAsc;

                int tryint;

                if ((Int32.TryParse(Request.QueryString["order"], out tryint)) && model.Orders.ContainsKey(Int32.Parse(Request.QueryString["order"])))
                {
                    selected_order = (ProductSort)Int32.Parse(Request.QueryString["order"]);
                }

                Dictionary<ProductFilter, object> filters = new Dictionary<ProductFilter, object>();
                if ((Int32.TryParse(Request.QueryString["min_price"], out tryint)))
                {
                    filters.Add(ProductFilter.PriceMin, Int32.Parse(Request.QueryString["min_price"]));
                }
                if ((Int32.TryParse(Request.QueryString["max_price"], out tryint)))
                {
                    filters.Add(ProductFilter.PriceMax, Int32.Parse(Request.QueryString["max_price"]));
                }

                model.Products = dataSource.GetCategoryProducts(id, page, selected_order, filters);

                CategoryView caterory = dataSource.GetCategory(id);

                if (caterory != null)
                {
                    model.Breadcrumbs = this.GetCategoryBreadcrumbs(id);
                    string category_name = caterory.Name;
                    model.Title = String.Format(Translater.Translate("title_category_{0}"), category_name);

                    model.Category = new CategoryView()
                                         {
                                             Id = id
                                         };

                    result = View("CategoryShow", model);
                }
                else
                { 
                    result = this.Error404();
                }

            } else { //Omg, its empty
                result = this.Error404();
            }

            return result; 
        }
    }
}
